package com.kedsoft.docms.controller;


import com.kedsoft.docms.common.MessageResult;
import com.kedsoft.docms.entity.User;
import com.kedsoft.docms.service.UserService;
import com.kedsoft.docms.util.MD5Utils;
import com.kedsoft.docms.vo.UserVo;
import com.kedsoft.docms.vo.PageVo;
import com.kedsoft.docms.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2018-11-13
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "index")
    public String index() {
        return "user/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<User> getPageList(Pagination<User> pagination, @RequestParam(required = false) String username, @RequestParam(required = false) String nickname, @RequestParam(required = false) String email){
        Map<String, Object> query = new HashMap<>();
        query.put("username", username);
        query.put("nickname", nickname);
        query.put("email", email);
        userService.pageList(pagination,query);
        PageVo<User> pageVo = new PageVo<>(pagination.getItems(),pagination.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "form")
    public String form(){
        return "user/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(UserVo param){
        User bean = new User();
        bean.setUsername(param.getUsername());
        bean.setPassword(MD5Utils.getMD5String((param.getPassword()==null ||param.getPassword()=="")?"123456":param.getPassword()));
        bean.setNickname(param.getNickname());
        bean.setEmail(param.getEmail());
        bean.setStatus(param.getStatus());
        bean.setCrtime(new Date());
        userService.insert(bean);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(UserVo param){
        User bean = new User();
        bean.setUsername(param.getUsername());
        bean.setNickname(param.getNickname());
        bean.setEmail(param.getEmail());
        bean.setStatus(param.getStatus());
        bean.setId(param.getId());
        bean.setUptime(new Date());
        userService.updateById(bean);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        userService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        String[] id = ids.split(",");
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            userService.deleteById(intId);
        }
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "search")
    public String search(){
        return "user/search";
    }
}

